package cn.iocoder.yudao.module.erp.controller.admin.gi.gibj;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.gi.gibj.vo.GiBjPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.gi.gibj.vo.GiBjRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.gi.gibj.vo.GiBjSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.gi.GiBjDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.gi.GiBjDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.giitem.GiItemBjDO;
import cn.iocoder.yudao.module.erp.service.gi.GiBjService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - ERP北疆口岸仓库")
@RestController
@RequestMapping("/erp/gi-bj")
@Validated
public class GiBjController {

    @Resource
    private GiBjService giBjService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP北疆口岸仓库")
//    @PreAuthorize("@ss.hasPermission('erp:gi-bj:create')")
    public CommonResult<String> createGiBj(@Valid @RequestBody GiBjSaveReqVO createReqVO) {
        return success(giBjService.createGiBj(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP北疆口岸仓库")
//    @PreAuthorize("@ss.hasPermission('erp:gi-bj:update')")
    public CommonResult<Boolean> updateGiBj(@Valid @RequestBody GiBjSaveReqVO updateReqVO) {
        giBjService.updateGiBj(updateReqVO);
        return success(true);
    }

    @PutMapping("/examine")
    @Operation(summary = "ERP物流北疆仓库单据审核")
//    @PreAuthorize("@ss.hasPermission('erp:gi-bj:examine')")
    public CommonResult<Boolean> examineGiBj(@Valid @RequestBody GiBjDO giBjDO) {
        giBjService.examineGiBj(giBjDO);
        return success(true);
    }
    
    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP北疆口岸仓库")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:gi-bj:delete')")
    public CommonResult<Boolean> deleteGiBj(@RequestParam("id") String id) {
        giBjService.deleteGiBj(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP北疆口岸仓库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('erp:gi-bj:query')")
    public CommonResult<GiBjRespVO> getGiBj(@RequestParam("id") String id) {
        GiBjDO giBj = giBjService.getGiBj(id);
        return success(BeanUtils.toBean(giBj, GiBjRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP北疆口岸仓库分页")
//    @PreAuthorize("@ss.hasPermission('erp:gi-bj:query')")
    public CommonResult<PageResult<GiBjRespVO>> getGiBjPage(@Valid GiBjPageReqVO pageReqVO) {
        PageResult<GiBjDO> pageResult = giBjService.getGiBjPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GiBjRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP北疆口岸仓库 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:gi-bj:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGiBjExcel(@Valid GiBjPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GiBjDO> list = giBjService.getGiBjPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP北疆口岸仓库.xls", "数据", GiBjRespVO.class,
                        BeanUtils.toBean(list, GiBjRespVO.class));
    }

    // ==================== 子表（ERP物流北疆仓库货号详情） ====================

    @GetMapping("/gi-item-bj/list-by-no")
    @Operation(summary = "获得ERP物流北疆仓库货号详情列表")
    @Parameter(name = "no", description = "入库编号")
//    @PreAuthorize("@ss.hasPermission('erp:gi-bj:detail')")
    public CommonResult<List<GiItemBjDO>> getGiItemBjListByNo(@RequestParam("no") String no) {
        return success(giBjService.getGiItemBjListByNo(no));
    }
}